package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.ReviewListAdapter;
import com.dingwei.superiordiary.bean.ReviewListBean;
import com.dingwei.superiordiary.mvp.presenter.ReviewListPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.weight.MyRadioGroup;
import com.lihao.baselibrary.weight.TitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/26.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class ReviewListActivity extends BaseActivity implements Contract.ReviewListView {

    ReviewListPresenter reviewListPresenter = new ReviewListPresenter(this);
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.ll_show_evaluation)
    LinearLayout llShowEvaluation;
    @Bind(R.id.img_give_like)
    ImageView imgGiveLike;
    @Bind(R.id.data_layout)
    LinearLayout dataLayout;
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.rb_all)
    RadioButton rbAll;
    @Bind(R.id.rb_purchased)
    RadioButton rbPurchased;
    @Bind(R.id.rb_emption)
    RadioButton rbEmption;
    @Bind(R.id.m_radio_group)
    MyRadioGroup mRadioGroup;

    ReviewListAdapter adapter;
    String id;
    int page = 1;
    int type = 1;
    String status = "";
    boolean isLike;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_review_list;
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    protected void initView() {

        id = getIntent().getStringExtra("id");
        isLike = getIntent().getBooleanExtra("isLike", false);


        if (isLike) {
            imgGiveLike.setImageResource(R.drawable.img_give_like_select);
        } else {
            imgGiveLike.setImageResource(R.drawable.img_give_like_no);
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.line), 0));
        adapter = new ReviewListAdapter(BaseActivity.context);
        mRecyclerView.setAdapter(adapter);

    }

    @Override
    protected void initListener() {
        titleBar.setOnMenuListener(new TitleBar.OnMenuListener() {
            @Override
            public void onMenuClick() {
                reviewListPresenter.showSortPopupWindow(titleBar.getMenu());
            }
        });
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                reviewListPresenter.initData(id, page, type, status, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                reviewListPresenter.initData(id, page, type, status, true, false);
            }
        });
        adapter.setReplyInterface(new ReviewListAdapter.ReplyInterface() {
            @Override
            public void reply(int position) {
                if (!BaseActivity.value.getIsLogin()) {
                    intent = new Intent(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                    return;
                }
                ReviewListBean.DataBean.ListBean bean = adapter.getItem(position);
                reviewListPresenter.showEvaluationDialog(id, bean.id);
            }

            @Override
            public void likes(int position) {
                ReviewListBean.DataBean.ListBean bean = adapter.getItem(position);
                reviewListPresenter.commentLike(bean.id, position);
            }

            @Override
            public void more(int position) {
                ReviewListBean.DataBean.ListBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context, MoreReplyActivityGoods.class);
                intent.putExtra("id", id);
                intent.putExtra("pid", bean.id);
                startActivity(intent);
            }
        });

        mRadioGroup.setOnCheckedChangeListener(new MyRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(MyRadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_all:
                        status = "";
                        break;
                    case R.id.rb_purchased:
                        status = "1";
                        break;
                    case R.id.rb_emption:
                        status = "2";
                        break;
                }
                page = 1;
                reviewListPresenter.initData(id, page, type, status, true, true);
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.ll_show_evaluation, R.id.img_give_like, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_show_evaluation:
                if (!BaseActivity.value.getIsLogin()) {
                    intent = new Intent(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                    return;
                }
                reviewListPresenter.showEvaluationDialog(id, "");
                break;
            case R.id.img_give_like:
                reviewListPresenter.like(id);
                break;
            case R.id.no_net_btn:
                page = 1;
                reviewListPresenter.initData(id, page, type, status, true, true);
                break;
        }
    }

    @Override
    public void SetHotSort() {
        page = 1;
        type = 2;
        reviewListPresenter.initData(id, page, type, status, true, false);
    }

    @Override
    public void SetTimeSort() {
        page = 1;
        type = 1;
        reviewListPresenter.initData(id, page, type, status, true, false);
    }

    @Override
    public void OnRefresh(boolean isRefresh, ReviewListBean bean) {
        adapter.clear();
        adapter.addAll(bean.data.list);
        refreshLayout.finishRefresh();
        showData("success");
    }

    @Override
    public void OnLoadMore(boolean isRefresh, ReviewListBean bean) {
        adapter.addAll(bean.data.list);
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void OnFail() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        showData("fail");
    }

    @Override
    public void LikeSuccess() {
        isLike = !isLike;
        if (isLike) {
            imgGiveLike.setImageResource(R.drawable.img_give_like_select);
        } else {
            imgGiveLike.setImageResource(R.drawable.img_give_like_no);
        }
    }

    @Override
    public void commentLikeSuccess(int position) {
        ReviewListBean.DataBean.ListBean bean = adapter.getItem(position);
        bean.likes = bean.likes + 1;
        bean.is_liked = 2;
        adapter.notifyItemChanged(position, bean);
    }

    @Override
    public void saveCommentSuccess() {
        page = 1;
        reviewListPresenter.initData(id, page, type, status, true, true);
    }

    public void showData(String state) {
        if (state.equals("success")) {
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);

            if (adapter.getAllData() != null && adapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }
}
